package com.apps.epocket.config;

import able.guice.AbleContextListener;
import able.guice.AbleModule;
import com.apps.epocket.quartz.SchedulerManager;
import com.apps.epocket.stripes.config.PocketActionBeanContext;
import com.google.inject.Injector;
import javax.servlet.ServletContextEvent;
import net.sourceforge.stripes.action.ActionBeanContext;
import org.quartz.SchedulerException;


public class PocketContextListener extends AbleContextListener {    

    SchedulerManager mgr;

    protected void afterGuiceInit(Injector injector) {
        try {
            mgr = injector.getInstance(SchedulerManager.class);
            mgr.start();
            System.out.println("[SchedulerManager] Start ... ");
        } catch (SchedulerException ex) {
            throw new RuntimeException("Scheduler Failed to start", ex);
        }
    }

    protected AbleModule getAbleModule() {
        return new PocketModule();
    }

    protected Class<? extends ActionBeanContext> getActionBeanContextType() {
        return PocketActionBeanContext.class;
    }   
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            if (mgr != null) {
                System.out.println("[SchedulerManager] Shutdown ... ");
                mgr.shutdown();
            }
        } catch (SchedulerException ex) {
            throw new RuntimeException("Scheduler Failed to stop");
        }
    }
    
}
